﻿var App_TypeModel = $.extend(TradeModel, { 
	editor:{
	    title: "弹出框名称",
		winid:'App_Type_EditForm',
		formid:'App_Type_EditForm_fm',
		width:720,
		height:300
	},  
    postAction: 'add',
    url: '/sysmgr/type',
    postUrl: '',
    title: " ",
    idField: "typeId",
    titleField: "typeName",
    toolbar: 'App_Type_Toolbar',
    datagrid: 'App_Type_Grid' 
});  

App_TypeModel.FindRowById=function(id) {  
    return $('#App_Type_Grid').treegrid('find', id);
}

App_TypeModel.reload =function() {
	fun_ReloadApp_TypeList();
} 

function fun_DelApp_Type(id,title,state){ 
	if(state!='closed')
		App_TypeModel.del(id,title);
	else
		App_TypeModel.ShowMsg('提示','请先删除子节点');
}

function fun_SaveApp_Type (){ 
	App_TypeModel.submit();
}


function fun_ShowApp_TypeEditForm(id) { 
    if (typeof (id) != undefined && typeof (id) == 'object') {
        App_TypeModel.postAction = 'update';
        App_TypeModel.selRow = id;
    }
    else if (id == -2) {
        App_TypeModel.postAction = 'add';
        App_TypeModel.selRow = {};
        App_TypeModel.selRow.pTypeId = $('#roottypeid').val(); 
    }
    else if (id == -1) {
        App_TypeModel.postAction = 'add';
        App_TypeModel.selRow = {};
        var parentNode=$('#App_Type_Grid').treegrid('getSelected'); 
        if (parentNode ) {
            App_TypeModel.selRow.pTypeId = parentNode['typeId'];
        }
    }
    else {
        App_TypeModel.postAction = 'update';
        App_TypeModel.selRow = fun_FindRowById(id);
    }  
    
    $('#App_Type_EditForm_fm').form('clear' );
    $('#App_Type_EditForm_fm').form('load', App_TypeModel.selRow);
}
 

function fun_App_TypeSearchbox(value, name) {
    if (value == "" || value == null) {
        $.messager.alert('提示', '请输入关键字!', 'info');
    }
    else {
        $('#App_Type_Grid').datagrid('reload');
    }

}

function fun_ReloadApp_TypeList() {
    $('#App_Type_Grid').treegrid('reload');
}


function fun_ReloadNodeList() {
    var row = $('#App_Type_Grid').treegrid('getSelected');
    $('#App_Type_Grid').treegrid('reload', row['typeId']);
}


$(function () {
    $('#App_Type_Grid').treegrid({
        fit: true,
        border: false,
        singleSelect: true,
        fitColumns: true,
        method: 'post',
        url: Onluy.basePath()+App_TypeModel.url+'/tree',
        idField: App_TypeModel.idField,
        treeField: App_TypeModel.titleField, 
        loader:App_TypeModel.Loader,
	    loadFilter: App_TypeModel.loadfilter,  
        toolbar: '#App_Type_Toolbar', 
        columns:[[  
        { field: 'typeName', title: '名称', width: 300, formatter: function (value, row) { return value + '&nbsp;&nbsp;&nbsp;<span style="color:#cecece">(&nbsp;' + row['typeId'] + '&nbsp;)</span>'; } },
        //{ field: 'typeId', title: '编号', width: 120 },
        { field: 'typeMemo', title: '说明', width: 160 },
        { field: 'startDate', title: '生效日期', width: 100, align: 'center', sortable:true  },
        { field: 'endDate', title: '结束日期', width: 100, align: 'center'  },
        { field: 'ordernum', title: '排序', width: 60, align: 'center' }
        , {
		    field: 'opt', title: '操作', width: 50, align: 'center', rowspan: 2,
		    formatter: function (value, rec) {
		    	var id=rec[App_TypeModel.idField];
		    	var title=rec[App_TypeModel.titleField];
		    	var state=rec['state'];
		        return  '<img class="btn-16" src="'+Onluy.basePath()+'/Images/icos/16/delete.gif" onclick="fun_DelApp_Type(\'' + id + '\',\'' + title + '\',\'' + state + '\' );"   />';
		    }
		}
        ]],
        rowStyler: function (row) { 
            var nowdate = new Date(); 
            var endDate = new Date(row['endDate']);
            if (nowdate > endDate) {
                return 'color:red;';
            } 
        },
        onLoadSuccess: function (data) {
            if(data.success){
            }
            else if(data.msg){
                 $.messager.alert('提示', data.msg );
            }
       },
        onLoadError: function (res) {
            $.messager.alert('错误', res['responseText'], 'error');
        },
        onBeforeLoad: function (row, param) {  
            param['roottypeid'] = $('#roottypeid').val();
            param['name'] = $('#App_Type_ss').searchbox('getName');
            param['value'] = $('#App_Type_ss').searchbox('getValue'); 
        },
        onSelect: function (rowData) {
            fun_ShowApp_TypeEditForm(rowData);
        },
        onContextMenu: function (e, node) {
            e.preventDefault();
            // select the node
            $('#App_Type_Grid').treegrid('select', node['typeId']);
            // display context menu
            $('#treemm').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        }
    }); 
});

 
